package com.example.neutonote2.data.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.example.neutonote2.data.dao.NoteDao;
import com.example.neutonote2.data.dao.TagDao;
import com.example.neutonote2.data.db.NeuToNoteDatabase;
import com.example.neutonote2.data.model.Note;
import com.example.neutonote2.data.model.NoteTagCrossRef;
import com.example.neutonote2.data.model.NoteWithTags;
import com.example.neutonote2.data.model.Tag;

import java.util.Date;
import java.util.List;

/**
 * 笔记仓库类，用于处理数据操作
 */
public class NoteRepository {
    private final NoteDao noteDao;
    private final TagDao tagDao;
    private final LiveData<List<Note>> allNotes;
    private final LiveData<List<NoteWithTags>> allNotesWithTags;

    public NoteRepository(Application application) {
        NeuToNoteDatabase db = NeuToNoteDatabase.getDatabase(application);
        noteDao = db.noteDao();
        tagDao = db.tagDao();
        allNotes = noteDao.getAllNotes();
        allNotesWithTags = noteDao.getAllNotesWithTags();
    }

    // 获取所有笔记
    public LiveData<List<Note>> getAllNotes() {
        return allNotes;
    }
    
    // 获取所有带标签的笔记
    public LiveData<List<NoteWithTags>> getAllNotesWithTags() {
        return allNotesWithTags;
    }

    // 按ID获取笔记
    public LiveData<Note> getNoteById(int id) {
        return noteDao.getNoteById(id);
    }
    
    // 按ID获取带标签的笔记
    public LiveData<NoteWithTags> getNoteWithTags(int id) {
        return noteDao.getNoteWithTags(id);
    }

    // 按分类获取笔记
    public LiveData<List<Note>> getNotesByCategory(String category) {
        return noteDao.getNotesByCategory(category);
    }
    
    // 按分类获取带标签的笔记
    public LiveData<List<NoteWithTags>> getNotesWithTagsByCategory(String category) {
        return noteDao.getNotesWithTagsByCategory(category);
    }

    // 按ID列表获取带标签的笔记
    public LiveData<List<NoteWithTags>> getNotesWithTagsByIds(List<Integer> noteIds) {
        return noteDao.getNotesWithTagsByIds(noteIds);
    }

    // 搜索笔记
    public LiveData<List<Note>> searchNotes(String query) {
        return noteDao.searchNotes(query);
    }
    
    // 获取需要复习的笔记
    public LiveData<List<Note>> getNotesToReview() {
        return noteDao.getNotesToReview(new Date());
    }

    // 插入笔记
    public void insert(Note note) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            long noteId = noteDao.insert(note);
        });
    }
    
    // 插入笔记并关联标签
    public void insertNoteWithTags(Note note, List<Tag> tags) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            long noteId = noteDao.insert(note);
            
            // 添加标签关联
            for (Tag tag : tags) {
                tagDao.insertNoteTagCrossRef(new NoteTagCrossRef((int) noteId, tag.getId()));
            }
        });
    }

    // 更新笔记
    public void update(Note note) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            noteDao.update(note);
        });
    }
    
    // 更新笔记和标签
    public void updateNoteWithTags(Note note, List<Tag> tags) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            // 更新笔记
            noteDao.update(note);
            
            // 删除所有现有标签关联
            tagDao.deleteAllTagsForNote(note.getId());
            
            // 添加新的标签关联
            for (Tag tag : tags) {
                tagDao.insertNoteTagCrossRef(new NoteTagCrossRef(note.getId(), tag.getId()));
            }
        });
    }

    // 删除笔记
    public void delete(Note note) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            // 关联表会自动级联删除
            noteDao.delete(note);
        });
    }
    
    // 按ID删除笔记
    public void deleteById(int id) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            // 关联表会自动级联删除
            noteDao.deleteById(id);
        });
    }
} 